﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebAPIExample.Data;
using WebAPIExample.Data.Models;
using WebAPIExample.Models;

namespace WebAPIExample.Adapters.Data
{
    public class TodoDataAdapter : ITodoAdapter
    {
        public List<TodoViewModel> GetTodoViewModels()
        {
            List<TodoViewModel> models = null;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                models = db.Todos.Select(t => new TodoViewModel
                {
                    DateCompleted = t.DateCompleted,
                    IsCompleted = t.DateCompleted.HasValue,
                    TaskName = t.TaskName,
                    Timestamp = t.Timestamp
                }).ToList();
            }

            return models;
        }

        public void CreateTodoItem(CreateTodoViewModel model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Todo dbTodo = new Todo
                {
                    TaskName = model.TaskName,
                    Timestamp = DateTime.Now
                };

                db.Todos.Add(dbTodo);

                db.SaveChanges();
            }
        }
    }
}